// pages/photo/photo.js

import FormData from '../../utils/formData/index';
import{updateTravelerFaces} from "../../api/home"
import {
    updateTravelerFace,
    
} from "../../api/home"
const {
    globalData
} = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // globalData,
        src: '',
        takePhoto: 0,
        srcs: "",
        id: 0,
    },
    //查看人脸示意图
    cooklook() {
        wx.navigateTo({
            url: '../Manface/Manface',
        })
    },


    //点击重新选择
    buybtn() {
        this.setData({
            buybtn1: 0,
            src: ""
        })
    },
    //点击本地相册上传
    buybtn1() {
        this.setData({
            buybtn1: 1,
        })
        let that = this

        wx.getImageInfo({
            count: 1,
            type: 'image',
            sourceType: ['album'],
            success(res) {
                console.log(res);
                res.tempFiles.map((item) => {
                    that.setData({
                        src: item.path
                    })
                })
                // tempFilePath可以作为 img 标签的 src 属性显示图片
                //   const tempFilePaths = res.tempFiles.path
                that.setData({
                    src: res.tempFiles[0].path
                })
                //   console.log(that.data.src);
            }
        })
    },
    //点击拍照
    takePhoto() {
        this.setData({
            buybtn1: 1
        })
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                console.log(res);
                this.setData({
                    src: res.tempImagePath
                })
            }
        })
    },

    //返回上一级
    onClickLeft() {
        wx.navigateBack({
            delta: 1,
        })
    },

    //上传人脸图片
    uploadfile() {
        let formData = new FormData()
        console.log(this.data.src);
        formData.appendFile("file", this.data.src, "test");
        let data = formData.getData()
        console.log(data);
        let _this = this
        //    uploadFileq(this.data.src)
        wx.request({
            url: globalData.baseUrl + "/chongqing/api/file/uploadFile",
            data: data.buffer,
            header: {
                'content-type': data.contentType,
                appId: "wx9c25f8e317416cd5",
                token: wx.getStorageSync('token')
            },
            method: 'POST',
            timeout: 60000,
            success: (result) => {
                console.log(result);
                wx.showLoading({
                    title: '加载中',
                })
                if (result.data.code == 1000) {
                    wx.hideLoading({})
                    console.log(_this.data.id);
                    console.log(decodeURIComponent(result.data.data).toString());
                    var a = decodeURIComponent(result.data.data).toString()
                    console.log(a);
                    // wx.request({
                    //     header: {
                    //         'content-type': 'application/json',
                    //         appId: "wx9c25f8e317416cd5",
                    //         token: wx.getStorageSync('token')
                    //     },
                    //   url: globalData.baseUrl + "/chongqing/api/reservation/updateTravelerFace",
                    //   data:{
                    //     'id':this.data.id,
                    //     'faceUrl':result.data.data,
                    //   },
                    //   method: 'POST',
                    //   timeout: 60000,
                    //   success: (result) => {
                    //     console.log(result);
                    // }
                    // })

                    updateTravelerFace({
                        'id': this.data.id,
                        'faceUrl': result.data.data,
                    }).then((res) => {
                        wx.navigateBack({
                            delta: 1,
                        })
                        console.log(res);
                    })
                    // wx.navigateBack({
                    //     delta: 1,
                    // })
                } else {
                    wx.showModal({
                        title: '系统有误，请重试',
                    })
                }
            },
            fail: (res) => {},
            complete: (res) => {},
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        this.setData({
            id: Number(options.id)
        })
        console.log(this.data.id);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.setStorageSync('renlianimg', '')
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})